<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="部门:" prop="did">
        <el-select v-model="queryParams.did" placeholder="请选择" clearable size="mini">
          <el-option
            v-for="item in optionSelect"
            :key="item.id"
            :label="item.deptName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业名称:" prop="name">
        <el-input
          size="mini"
          v-model="queryParams.name"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业分类:" prop="category">
        <el-input
          size="mini"
          v-model="queryParams.category"
          placeholder="请输入企业分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini" v-hasPermi="['system:overtEnterprise:add']"
          @click="handleAdd"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="部门名称" prop="deptName" key="deptName" align="center"/>
      <el-table-column label="企业名称" prop="name" key="name" align="center"/>
      <el-table-column label="联系人" prop="liaison" key="liaison" align="center"/>
      <el-table-column label="联系电话" prop="liaisonTel" key="liaisonTel" align="center"/>
      <el-table-column label="企业分类" prop="category" key="category" align="center"/>
      <el-table-column label="排序号" prop="sort" key="sort" align="center"/>
      <el-table-column label="创建时间" prop="createTime" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" class-name="small-padding fixed-width" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:overtEnterprise:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:overtEnterprise:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="部门:" prop="did">
          <el-select v-model="form.did" placeholder="请选择部门" clearable style="width: 100%">
            <el-option
              v-for="item in optionSelect"
              :key="item.id"
              :label="item.deptName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="企业名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称"/>
        </el-form-item>
        <el-form-item label="企业名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称"/>
        </el-form-item>
        <el-form-item label="联系人:" prop="liaison">
          <el-input v-model="form.liaison" placeholder="请输入联系人"/>
        </el-form-item>
        <el-form-item label="联系电话:" prop="liaisonTel">
          <el-input v-model="form.liaisonTel" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="企业分类:" prop="category">
          <el-input v-model="form.category" placeholder="请输入企业分类"/>
        </el-form-item>
        <el-form-item label="显示排序:" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { enterpriseRemove,getEnterpriseInfo,enterpriseEdit,enterpriseAdd,enterprisePageList,optionSelect } from '@/api/triones/overt';
import {getToken} from "@/utils/auth";
export default {
  name: "index",
  data() {
    return {
      showSearch: true,
      total: 0,
      dataList: [],
      loading: false,
      optionSelect: [],
      optionChildSelect: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        did: null,
        name: null,
        category: null
      },
      title: '',
      open: false,
      form: {},
      rules: {
        did: [
          {required: true, message: "部门不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "企业名称不能为空", trigger: "blur"}
        ]
      },
      upload: {
        open: false,
        title: "",
        isUploading: false,
        updateSupport: 0,
        headers: {Authorization: "Bearer " + getToken()},
        url: process.env.VUE_APP_BASE_API + "/system/overt/importData"
      },
    }
  },
  created() {
    this.getList();
    this.getOptionSelect();
  },
  methods: {
    getOptionSelect() {
      optionSelect().then(res => {
        this.optionSelect = res.data;
      });
    },
    getList() {
      this.loading = true;
      enterprisePageList(this.queryParams).then(res => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.getOptionSelect();
      this.reset();
      this.title = "添加企业";
      this.open = true;
    },
    handleEdit(row) {
      this.reset();
      const id = row.id;
      getEnterpriseInfo(id).then(response => {
        this.form = response.data;
        this.title = "修改企业";
        this.open = true;
      })
    },
    reset() {
      this.form = {
        id: undefined,
        did: undefined,
        name: undefined,
        category: undefined,
        sort: 0,
        liaison: undefined,
        liaisonTel: undefined
      }
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.open = true;
          if (this.form.id != null) {
            enterpriseEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            enterpriseAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除企业名称为"' + row.name + '"的数据项？').then(function () {
        return enterpriseRemove(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleImport() {
      this.upload.title = "公开企业导入";
      this.upload.open = true;
    },
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
}
</script>

<style scoped>

</style>
